<?php

class TestDateTimeBean extends UnitTestCase {

	function testConstruct() {
		$now = new DateTimeBean();

		$now2=DateTimeBean::now();
		$this->assertNoErrors();
		$this->assertEqual($now,$now2);

		$this->assertEqual($now->seconds,intval(date('s')),'should have an accurate seconds attribute');
		$this->assertEqual($now->minutes,intval(date('i')),'should have an accurate minutes attribute');
		$this->assertEqual($now->hours,intval(date('H')),'should have an accurate hours attribute');
		$this->assertEqual($now->mday,intval(date('d')),'should have an accurate mday attribute');
		$this->assertEqual($now->mon,intval(date('m')),'should have an accurate mon attribute');
		$this->assertEqual($now->year,intval(date('Y')),'should have an accurate year attribute');
	}

	function testGettersSetters() {
		$d = new DateTimeBean();
		$this->assertTrue($d->seconds(37),'should have a seconds() setter');
		$this->assertTrue($d->minutes(15),'should have a minutes() setter');
		$this->assertTrue($d->hours(16),'should have a hours() setter');
		$this->assertTrue($d->mday(10),'should have a mday() setter');
		$this->assertTrue($d->mon(1),'should have a mon() setter');
		$this->assertTrue($d->year(1976),'should have a year() setter');

		$this->assertEqual($d->seconds(),37,'should have an accurate seconds getter');
		$this->assertEqual($d->minutes(),15,'should have an accurate minutes getter');
		$this->assertEqual($d->hours(),16,'should have an accurate hours getter');
		$this->assertEqual($d->mday(),10,'should have an accurate mday getter');
		$this->assertEqual($d->mon(),1,'should have an accurate mon getter');
		$this->assertEqual($d->year(),1976,'should have an accurate year getter');
	}

	function testLocales() {

	}

	function testInputs() {
		$d=new DateTimeBean();

		/**
		 *ISO 8601
		 * @link http://fr.wikipedia.org/wiki/ISO_8601
		 * @todo tests for variations : short, extended formats, timezones
		 */
		$this->assertTrue($d->setFromInput('19760110T16:15:37Z'),'should accept ISO8601 input format');
		$this->compare($d,1976,1,10,16,15,37);
		$d2=new DateTimeBean('19760110T16:15:37Z');
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');

		// MySQL datetime
		$this->assertTrue($d->setFromInput('1976-01-10 16:15:37'),'should accept MySQL datetime input format');
		$this->compare($d,1976,1,10,16,15,37);
		$d2=new DateTimeBean('1976-01-10 16:15:37');
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');

		// MySQL date
		$this->assertTrue($d->setFromInput('1976-01-10'),'should accept MySQL date input format');
		$this->compare($d,1976,1,10,0,0,0);
		$d2=new DateTimeBean('1976-01-10');
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');

		// MySQL timestamp
		$this->assertTrue($d->setFromInput('19760110161537'),'should accept MySQL timestamp input format');
		$this->compare($d,1976,1,10,16,15,37);
		$d2=new DateTimeBean('19760110161537');
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');

		// PHP/unix timestamp 190134937
		$this->assertTrue($d->setFromInput(190134937),'should accept PHP/Unix timestamp input format');
		$this->compare($d,1976,1,10,16,15,37);
		$d2=new DateTimeBean(190134937);
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');

		// PHP getdate
		$getdate=array('year'=>1976,'mon'=>1,'mday'=>10,'hours'=>16,'minutes'=>15,'seconds'=>37);
		$this->assertTrue($d->setFromInput($getdate),'should accept PHP getdate() result input format');
		$this->compare($d,1976,1,10,16,15,37);
		$d2=new DateTimeBean($getdate);
		$this->assertEqual($d,$d2,'Constructor should accept same formats as setFromInput()');


		// french date
	}

	function testOutputs() {
		$d=new DateTimeBean();

		//$this->assertEqual($d->toString('%Y %y %m %d %H %i %s %j %w'),date('Y y m d H i s z w'),'invalid toString() output %s');
		// weird, is there a bug for year day ?
		$this->assertEqual($d->toString('%Y %y %m %d %H %i %s %w'),date('Y y m d H i s w'),'invalid toString() output %s');

		$this->assertEqual($d->toMysqlDate(),date('Y-m-d'),'invalid toMysqlDate() output %s');
		$this->assertEqual($d->toMysqlDateTime(),date('Y-m-d H:i:s'),'invalid toMysqlDateTime() output %s');
		$this->assertEqual($d->toXmlRpc(),date('Ymd\TH:i:s'),'invalid toXmlRpc() output %s');
		$this->assertEqual($d->toTimestamp(),time(),'invalid toTimestamp() output %s');
	}

	function testAutocorrect() {

	}

	/**
	 * add(), addSeconds(), addDays(), addYears()
	 */
	function testAdd() {

	}

	function testDaysDistance() {

	}

	function testDaysInMonth() {

	}

	function testDaysInYear() {

	}

	function testYearDay() {

	}

	function testWeekDay() {

	}

	function testWeekNumber() {

	}

	/**
	 * isPast(), isFuture(), isBefore(), isAfter()
	 */
	function testCompare() {

	}

	function testRandom() {

	}

	/**
	 * compare a DateTimeBean with a date values
	 * @param DateTimeBean $bean
	 * @param int $year
	 * @param int $mon
	 * @param int $mday
	 * @param int $hours
	 * @param int $minutes
	 * @param int $seconds
	 */
	function compare($d,$year,$mon,$mday,$hours,$minutes,$seconds) {
		if (!$this->assertEqual($d->seconds,$seconds,'%s')
		or !$this->assertEqual($d->minutes,$minutes,'%s')
		or !$this->assertEqual($d->hours,$hours,'%s')
		or !$this->assertEqual($d->mday,$mday,'%s')
		or !$this->assertEqual($d->mon,$mon,'%s')
		or !$this->assertEqual($d->year,$year,'%s')) {
			$this->fail("date is not equal to $year,$mon,$mday,$hours,$minutes,$seconds");
			$this->dump($d);
			return false;
		}
		return true;
	}
}
?>